package io.renren.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import io.renren.dao.TPurviewCustinfoDao;
import io.renren.entity.SysUserEntity;
import io.renren.entity.TPurviewCustinfoEntity;
import io.renren.entity.TPurviewUsermanageEntity;
import io.renren.entity.User;
import io.renren.service.SysUserService;
import io.renren.service.TPurviewCustinfoService;
import io.renren.service.TPurviewUsermanageService;
import io.renren.utils.ShiroUtils;
import io.renren.utils.httpclient.HttpRequestUtils;
import io.renren.utils.httpclient.UUIDUitl;



@Service("tPurviewCustinfoService")
@Transactional(rollbackFor=Throwable.class)
public class TPurviewCustinfoServiceImpl implements TPurviewCustinfoService {
	@Autowired
	private TPurviewCustinfoDao tPurviewCustinfoDao;
	@Autowired
	private TPurviewUsermanageService tPurviewUsermanageService;
	@Autowired
	private SysUserService sysUserService;
	
	private static String  ipconfig = "ycwip.properties";
	
	private String getUrlIp(){
		Properties  properties =HttpRequestUtils.loadPropertiesFile(ipconfig);
		String ip =  (String) properties.get("ip");
		return ip ;
	}
	@Override
	public TPurviewCustinfoEntity queryObject(String id){
		return tPurviewCustinfoDao.queryObject(id);
	}
	
	@Override
	public List<TPurviewCustinfoEntity> queryList(Map<String, Object> map){
		return tPurviewCustinfoDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return tPurviewCustinfoDao.queryTotal(map);
	}
	
	@Override
	public void save(TPurviewCustinfoEntity tPurviewCustinfo){
            String id = UUIDUitl.getuuid() ;
			tPurviewCustinfo.setId(id);
			SysUserEntity obj = ShiroUtils.getUserEntity();
			tPurviewCustinfo.setCreatname(obj.getUsername());
			tPurviewCustinfo.setCreatdate(new Date());
			tPurviewCustinfoDao.save(tPurviewCustinfo);
			tPurviewUsermanageService.save(changeToTPurviewUsermanageEntity(tPurviewCustinfo,id));
			sysUserService.save(changeToSysUserEntity(tPurviewCustinfo));
	}
	private User changeToUser(TPurviewCustinfoEntity tPurviewCustinfo ){
		User user = new User();
		user.setId("8d8c17e9ac224d289cbb05b2d3d41172");
		user.setNumber(tPurviewCustinfo.getUsername());
		user.setName(tPurviewCustinfo.getName());
		return user; 
	}
	private TPurviewUsermanageEntity changeToTPurviewUsermanageEntity(TPurviewCustinfoEntity tPurviewCustinfo,String custid){
		TPurviewUsermanageEntity tPurviewUsermanageEntity =new TPurviewUsermanageEntity();
		tPurviewUsermanageEntity.setIsadmin("1");
		tPurviewUsermanageEntity.setStatus("0");
		SysUserEntity obj = ShiroUtils.getUserEntity();
		tPurviewUsermanageEntity.setCustid(obj.getUserId().toString());
		tPurviewUsermanageEntity.setUsername(tPurviewCustinfo.getUsername());
		if(isChinaPhoneLegal(tPurviewCustinfo.getUsername())) {
		tPurviewUsermanageEntity.setPassword(tPurviewCustinfo.getUsername().substring(5)); 
		}else {
			tPurviewUsermanageEntity.setPassword(tPurviewCustinfo.getUsername()); 
		}
		tPurviewUsermanageEntity.setCustid(custid);
		tPurviewUsermanageEntity.setIsdeleted("0");
		tPurviewUsermanageEntity.setCreatedate(new Date());
	   return tPurviewUsermanageEntity ;
	}
	private  SysUserEntity changeToSysUserEntity(TPurviewCustinfoEntity tPurviewCustinfo){
		SysUserEntity sysUserEntity = new SysUserEntity();
		sysUserEntity.setCreateTime(new Date());
		sysUserEntity.setUsername(tPurviewCustinfo.getUsername());
		sysUserEntity.setStatus(1);
		sysUserEntity.setCreateUserId(1l);
		List<Long> list = new ArrayList<Long>();
		list.add(1l);
		sysUserEntity.setRoleIdList(list);
		if(isChinaPhoneLegal(tPurviewCustinfo.getUsername())) {
		sysUserEntity.setMobile(tPurviewCustinfo.getUsername());
		sysUserEntity.setPassword(tPurviewCustinfo.getUsername().substring(5));
		}else{
			sysUserEntity.setPassword(tPurviewCustinfo.getUsername());
		}
		
		return  sysUserEntity; 
	}
	public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {  
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";  
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher(str);  
        return m.matches();  
    }  
	@Override
	public void update(TPurviewCustinfoEntity tPurviewCustinfo){
		tPurviewCustinfoDao.update(tPurviewCustinfo);
	}
	
	@Override
	public void delete(String id){
		tPurviewCustinfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		tPurviewCustinfoDao.deleteBatch(ids);
	}
	@Override
	public List<TPurviewCustinfoEntity> CustinfoList() {
		return tPurviewCustinfoDao.custinfoList();
	}
	@Override
	public TPurviewCustinfoEntity getinfoByUserName(String username) {
		return tPurviewCustinfoDao.getinfoByUserName(username);
	}
	
}
